package com.learn.springbootnetty.占用字节数;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

/**
 * @author xiaodonghong
 * @description TODO
 * @date 2023/6/15-14:42
 */
public class GBKDemo {
    public static void main(String[] args) throws UnsupportedEncodingException {
        main2();
    }


    public static void main1() throws UnsupportedEncodingException {
        String a = "名";
        System.out.println();
        System.out.println("UTF-8编码长度:"+a.getBytes("UTF-8").length);
        System.out.println("UTF-16编码长度:"+a.getBytes("UTF-16").length);
        System.out.println("GBK编码长度:"+a.getBytes("GBK").length);
        System.out.println("GB2312编码长度:"+a.getBytes("GB2312").length);

        System.out.println("-----------------分割线--------------------");
        String b = "B";
        System.out.println("UTF-8编码长度:"+b.getBytes("UTF-8").length);
        System.out.println("UTF-16编码长度:"+b.getBytes("UTF-16").length);
        System.out.println("GBK编码长度:"+b.getBytes("GBK").length);
        System.out.println("GB2312编码长度:"+b.getBytes("GB2312").length);
    }


    public static void main2() throws UnsupportedEncodingException {
        String a = "名字";
        System.out.println();
        byte[] utf8byte = a.getBytes("UTF-8");
        String str = new String(utf8byte, Charset.forName("UTF-8"));
        System.out.println(utf8byte+"   UTF-8编码长度:"+utf8byte.length);
      //   System.out.println(a.getBytes("UTF-16") +" UTF-16编码长度:"+a.getBytes("UTF-16").length);

        byte[] gbkByte = a.getBytes("GBK");
        System.out.println(gbkByte + "  GBK编码长度:"+gbkByte.length);
        String str2 = new String(gbkByte, Charset.forName("GBK"));
        // System.out.println(a.getBytes("GB2312") + "GB2312编码长度:"+a.getBytes("GB2312").length);

        System.out.println("-----------------分割线--------------------");
        String b = "Bb";
        System.out.println(b.getBytes("UTF-8")+ " ;UTF-8编码长度:"+b.getBytes("UTF-8").length);
        System.out.println(b.getBytes("UTF-16")+ " ;UTF-16编码长度:"+b.getBytes("UTF-16").length);
        byte[] gsbkbyte = b.getBytes("GBK");
        System.out.println(gsbkbyte + "  GBK编码长度:"+gsbkbyte.length);
        System.out.println(b.getBytes("GBK")+" ;GBK编码长度:"+b.getBytes("GBK").length);
        String sbc = new String(gsbkbyte, Charset.forName("GBK"));
        System.out.println(b.getBytes("GB2312") + "   ;GB2312编码长度:"+b.getBytes("GB2312").length);


        System.out.println("-----------------分割线--------------------");
        String c = "Bb主力";
        System.out.println(c.getBytes("UTF-8")+ " UTF-8编码长度:"+c.getBytes("UTF-8").length);
        System.out.println("UTF-16编码长度:"+c.getBytes("UTF-16").length);
        System.out.println(c.getBytes("GBK")+ "GBK编码长度:"+c.getBytes("GBK").length);
        System.out.println("GB2312编码长度:"+c.getBytes("GB2312").length);
    }
}
